<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="目标类型" prop="targetType">
        <el-select v-model="queryParams.targetType" placeholder="请选择目标类型" clearable>
          <el-option v-for="dict in dict.type.comments_target_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="作品" prop="workTitle">
        <el-input v-model="queryParams.workTitle" placeholder="请输入作品" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="作品作者" prop="commentAuthorNickname">
        <el-input v-model="queryParams.commentAuthorNickname" placeholder="请输入作品作者" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="评论用户" prop="userNickname">
        <el-input v-model="queryParams.userNickname" placeholder="请输入评论用户" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="评论内容" prop="content">
        <el-input v-model="queryParams.content" placeholder="请输入评论内容" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['foodworks:comments:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['foodworks:comments:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['foodworks:comments:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['foodworks:comments:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="multiple"
          @click="handleBatchAudit"
          v-hasPermi="['foodworks:comments:audit']"
        >批量审核</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commentsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="目标ID" align="center" prop="targetId" /> -->
      <el-table-column label="作品" align="center" prop="workTitle" />
      <el-table-column label="作品作者" align="center" prop="workAuthorNickname" />
      <!-- <el-table-column label="评论ID" align="center" prop="id" /> -->
      <!-- <el-table-column label="评论用户ID" align="center" prop="userId" /> -->
      <el-table-column label="评论用户" align="center" prop="userNickname" />
      <el-table-column label="评论内容" align="center" prop="content" />
      <el-table-column label="评论点赞数" align="center" prop="likeCount" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="warning">待审核</el-tag>
          <el-tag v-else-if="scope.row.status === 1" type="success">已通过</el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="danger">已拒绝</el-tag>
          <el-tag v-else type="info">未知状态({{ scope.row.status }})</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="目标类型" align="center" prop="targetType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.comments_target_type" :value="scope.row.targetType" />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['foodworks:comments:edit']"
          >修改</el-button> -->
          <!-- 添加审核功能-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleAudit(scope.row)"
            v-hasPermi="['foodworks:comments:audit']"
            v-if="scope.row.status === 0"
          >审核</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['foodworks:comments:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改作品评论对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->

    <!-- 审核评论对话框 -->
    <el-dialog :title="'审核评论 - ID: ' + auditForm.id" :visible.sync="auditOpen" width="600px" append-to-body>
      <el-form ref="auditForm" :model="auditForm" label-width="80px">
        <el-form-item label="作品">
          <span>{{ auditForm.workTitle }}</span>
        </el-form-item>
        <el-form-item label="作品作者">
          <span>{{ auditForm.workAuthorNickname }}</span>
        </el-form-item>
        <el-form-item label="评论用户">
          <span>{{ auditForm.userNickname }}</span>
        </el-form-item>
        <el-form-item label="评论内容">
          <div style="max-height: 200px; overflow-y: auto; border: 1px solid #dcdfe6; padding: 10px; border-radius: 4px;">
            {{ auditForm.content }}
          </div>
        </el-form-item>
        <el-form-item label="评论时间">
          <span>{{ parseTime(auditForm.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </el-form-item>
        <el-form-item label="点赞数">
          <span>{{ auditForm.likeCount }}</span>
        </el-form-item>
        <el-form-item label="审核结果" prop="auditStatus">
          <el-radio-group v-model="auditForm.auditStatus">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核备注" prop="auditRemark">
          <el-input
            v-model="auditForm.auditRemark"
            type="textarea"
            :rows="3"
            placeholder="请输入审核备注（可选）"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAudit">确 定</el-button>
        <el-button @click="cancelAudit">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listComments,
    getComments,
    delComments,
    addComments,
    updateComments,
    auditComments,
    batchAuditComments
  } from "@/api/foodworks/comments"

  export default {
    name: "Comments",
    dicts: ['comments_target_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 作品评论表格数据
        commentsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userId: null,
          targetType: null,
          targetId: null,
          content: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        // 审核对话框相关
        auditOpen: false,
        auditForm: {
          id: null,
          workTitle: null,
          workAuthorNickname: null,
          userNickname: null,
          content: null,
          createTime: null,
          likeCount: null,
          auditStatus: null,
          auditRemark: null
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询作品评论列表 */
      getList() {
        this.loading = true
        listComments(this.queryParams).then(response => {
          this.commentsList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          userId: null,
          targetType: null,
          targetId: null,
          content: null,
          likeCount: null,
          createTime: null,
          del: null
        }
        this.resetForm("form")
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm")
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      // /** 新增按钮操作 */
      // handleAdd() {
      //   this.reset()
      //   this.open = true
      //   this.title = "添加作品评论"
      // },
      // /** 修改按钮操作 */
      // handleUpdate(row) {
      //   this.reset()
      //   const id = row.id || this.ids
      //   getComments(id).then(response => {
      //     this.form = response.data
      //     this.open = true
      //     this.title = "修改作品评论"
      //   })
      // },
      // /** 提交按钮 */
      // submitForm() {
      //   this.$refs["form"].validate(valid => {
      //     if (valid) {
      //       if (this.form.id != null) {
      //         updateComments(this.form).then(response => {
      //           this.$modal.msgSuccess("修改成功")
      //           this.open = false
      //           this.getList()
      //         })
      //       } else {
      //         addComments(this.form).then(response => {
      //           this.$modal.msgSuccess("新增成功")
      //           this.open = false
      //           this.getList()
      //         })
      //       }
      //     }
      //   })
      // },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$modal.confirm('是否确认删除作品评论编号为"' + ids + '"的数据项？').then(function() {
          return delComments(ids)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        }).catch(() => {})
      },
      /** 导出按钮操作 */
      // handleExport() {
      //   this.download('foodworks/comments/export', {
      //     ...this.queryParams
      //   }, `comments_${new Date().getTime()}.xlsx`)
      // }

      // 审核功能
      handleAudit(row) {
        this.auditForm = { ...row };
        this.auditOpen = true;
      },
      cancelAudit() {
        this.auditOpen = false;
        this.resetAuditForm();
      },
      resetAuditForm() {
        this.auditForm = {
          id: null,
          workTitle: null,
          workAuthorNickname: null,
          userNickname: null,
          content: null,
          createTime: null,
          likeCount: null,
          auditStatus: null,
          auditRemark: null
        };
        this.resetForm("auditForm");
      },
             submitAudit() {
         this.$refs["auditForm"].validate(valid => {
           if (valid) {
             const auditData = {
               commentId: this.auditForm.id,
               status: this.auditForm.auditStatus,
               remark: this.auditForm.auditRemark
             }
             auditComments(auditData).then(response => {
               this.$modal.msgSuccess("审核成功")
               this.auditOpen = false
               this.getList()
             }).catch(error => {
               this.$modal.msgError(error.msg)
             })
           }
         })
       },
       /** 批量审核操作 */
       handleBatchAudit() {
         this.$modal.prompt('请输入审核备注（可选）', '批量审核', {
           confirmButtonText: '确定',
           cancelButtonText: '取消',
           inputPlaceholder: '请输入审核备注'
         }).then(({ value }) => {
           const auditData = {
             commentIds: this.ids,
             status: 1, // 默认通过
             remark: value || ''
           }
           return batchAuditComments(auditData)
         }).then(() => {
           this.getList()
           this.$modal.msgSuccess("批量审核成功")
         }).catch(() => {})
       }
    }
  }
</script>
